﻿#include "Boa.h"
#include "cocos2d.h"
#include "cocos-ext.h"
#include "Config.h"

using namespace cocos2d;
using namespace cocos2d::extension;



Boa * Boa::create(CCNode* monkeyNode)
{
	Boa *boa = new Boa();
    if (boa && boa->init(monkeyNode))
    {
        boa->autorelease();
        return boa;
    }
    else
    {
        CC_SAFE_DELETE(boa);
        return NULL;
    }
}
bool Boa::init(CCNode* boaNode)
{
	CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo(kImageBoa, kPlistBoa, kJsonBoa);
	m_Armature = CCArmature::create(kNameBoa);
	m_Armature->setPosition(0,0);
	m_Armature->setScale(0.4);
    this->addChild(m_Armature,500); 

	// Trang thai ban dau cua con khi
	m_currentStatus = CRAWLING;
    m_newState = CRAWLING;

	// Pre load music
	fPreLoadMusic();

	this->schedule(schedule_selector(Boa::update));
	return true;
}

// Preload tat ca cac music effect
void Boa::fPreLoadMusic()
{
	CCLog("PreLoad music !!!!");
	//CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(kSoundBroken);
}
// Update cac trang thai cua con khi
void Boa::update(float dt)
{

	if (m_currentStatus == m_newState){
		return;
	}else // cap nhat lai animation
    {
		CCLog("Co thay doi trang thai");
        m_currentStatus = m_newState;
        fEffect();
    }
}

void Boa::fEffect()
{
	switch(m_currentStatus)
	{
			//action
			//play sound effect here

		case CRAWLING: //bo
			m_Armature->getAnimation()->play("duday"); 
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		case BITE: //can
			m_Armature->getAnimation()->play("duday"); 
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		case BODYFLY: //phithan
			m_Armature->getAnimation()->play("duday"); 
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		default: 
			CCLog("Default effect broken !!");
			break;
	}
}


// Amimation callback cua con khi
void Boa::onAnimationEvent(cocos2d::extension::CCArmature *pArmature, cocos2d::extension::MovementEventType eventType, const char *animationID)
{
	std::string id = animationID;
    if (eventType == COMPLETE)
    {
		CCLog("Animation complete");
		CCLog("Animation ID = %d",animationID);

        if (id.compare("broken") == 0)
        {	
			CCLog("It's broken !!");
			pArmature->getAnimation()->stop();// Stop animation when loop complate
        }
    }

}


void Boa::fSetPosition(CCPoint point)
{
	CCNode::setPosition(point);
}

void Boa::fSetPosition(float x, float y)
{
	CCNode::setPosition(x,y);
}

CCPoint Boa::fGetPosition()
{
	return CCNode::getPosition();
}